package vlms.util;

import java.sql.DriverManager;
import java.util.Random;

import java.sql.Connection;

import vlms.dao.PremiumcustomerDAO;
import vlms.dao.SimplecustomerDAO;

public class MemberNumGen {

	private static final int minimum = 10000000;
	private static final int maximum = 99999999;
	public static int generateMemberNum()
	{
		int result = 0;
		try
		{
			Connection con = DriverManager.getConnection("jdbc:user_mysql:");
			SimplecustomerDAO sc = new SimplecustomerDAO();
			PremiumcustomerDAO pm = new PremiumcustomerDAO();
			
			Random random = new Random(maximum - minimum);
			result = minimum + random.nextInt();
			// Changed result to String : Mandar
			while (sc.findByPrimaryKey(String.valueOf(result), con) != null || 
				   pm.findByPrimaryKey(String.valueOf(result), con) != null)
			{
				result = minimum + random.nextInt();
			}
		}
		catch(Exception e)
		{
			// Ignore the exception and return whatever result currently is
			// been populated.
		}
		return result;
	}
}
